const { SocksProxyAgent } = require('socks-proxy-agent')
const axios = require('axios')
const fs = require('fs')
const FormData = require('form-data')

const agent = new SocksProxyAgent('socks5://192.168.11.99:7890')

// 解析简历数据
async function parseResume (filePath) {
  const form = new FormData()
  form.append('resume-file', await fs.createReadStream(filePath))
  const RESUME_URL = 'https://www.xiaoxizn.com/portrait-upload?parse_mode=general'
  const response = await axios.post(RESUME_URL, form, {
    httpAgent: agent,
    httpsAgent: agent,
    headers: {
      ...form.getHeaders(),
      'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
  })
  return response.data
}

/**
 * 提取整理简历解析信息
 * @param {*} doc 简历文档
 * @returns {Promise<null|{desired_salary, num_work_experience, education, gender, date_of_birth, expect_location, desired_position, memo: string, detailed_location, professional, project_experience, ID: string, email, work_experience, professional_level, wechat, degree, work_experience_count, school_name, predicted_skills, dataNull: null, highlights, birthplace, name, phone_number, age}>}
 */
async function getResumeInfo (doc) {
  // 数据不完整则跳过
  if (!doc || !doc.parsing_result || !doc.parsing_result.basic_info) {
    return null
  }
  const parsingResult = doc.parsing_result
  const predictedResult = doc.predicted_result
  const basicInfo = parsingResult.basic_info
  const contactInfo = parsingResult.contact_info
  const projectExperience = parsingResult.project_experience

  return {
    // dataNull: null,
    // 数据字段
    name: basicInfo.name,
    phone_number: contactInfo.phone_number,
    wechat: contactInfo.wechat,
    email: contactInfo.email,
    birthplace: basicInfo.birthplace,
    detailed_location: basicInfo.detailed_location,
    date_of_birth: basicInfo.date_of_birth,
    age: basicInfo.age,
    gender: basicInfo.gender,
    project_experience: projectExperience.map(e => e.project_name).join('\r\n'),
    degree: basicInfo.degree,
    expect_location: basicInfo.expect_location,
    desired_salary: basicInfo.desired_salary,
    desired_position: basicInfo.desired_position,
    professional_level: basicInfo.professional_level,
    professional: predictedResult.tags.professional.map(e => e.tag).join(' '),
    work_experience: parsingResult.work_experience
      .map(e => `${e.company_name} ${e.start_time_year}.${e.start_time_month}` +
        ` - ${e.end_time_year}.${e.end_time_month}`).join('\r\n'),
    work_experience_count: parsingResult.work_experience.length,
    school_name: basicInfo.school_name,
    education: predictedResult.tags.education.map(e => e.tag).join(' '),
    highlights: predictedResult.highlights.tags.map(e => e).join(' '),
    memo: (() => {
      const highlights = predictedResult.highlights
      return [
        highlights.occupation.join(' '),
        highlights.project.join(' '),
        highlights.education.join(' '),
        highlights.others.join(' ')
      ].filter(e => e).join('\r\n')
    })(),
    num_work_experience: basicInfo.num_work_experience,
    predicted_skills: predictedResult.predicted_skills
      .filter(e => e.score >= 0.8)
      .map(e => `${e.skill}:${e.score * 10}`).join(' ')
  }
}

module.exports = {
  getResumeInfo,
  parseResume
}
